package com.lcke.common.util;

import cn.hutool.core.util.StrUtil;
import com.lcke.common.login.LoginCache;
import com.lcke.common.login.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class HttpContext {

    /**
     * 获取请求的ip地址
     *
     * @author shenmk
     * @Date 2020/7/23 下午3:44
     */
    public static String getIp() {
        HttpServletRequest request = HttpContext.getRequest();
        if (request == null) {
            return "127.0.0.1";
        } else {
            return request.getRemoteHost();
        }
    }

    /**
     * 获取当前请求的Request对象
     *
     * @author shenmk
     * @Date 2020/7/23 下午3:44
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        } else {
            return requestAttributes.getRequest();
        }
    }

    /**
     * 获取当前请求的Response对象
     *
     * @author shenmk
     * @Date 2020/7/23 下午3:44
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        } else {
            return requestAttributes.getResponse();
        }
    }

    /**
     * 获取所有请求的值
     *
     * @author shenmk
     * @Date 2020/7/23 下午3:44
     */
    public static Map<String, String> getRequestParameters() {
        HashMap<String, String> values = new HashMap<>();
        HttpServletRequest request = HttpContext.getRequest();
        if (request == null) {
            return values;
        }
        Enumeration enums = request.getParameterNames();
        while (enums.hasMoreElements()) {
            String paramName = (String) enums.nextElement();
            String paramValue = request.getParameter(paramName);
            values.put(paramName, paramValue);
        }
        return values;
    }

    /**
     * 从session中获取当前登录用户对象
     *
     * @author shenmk
     * @Date 2020/7/23 下午3:44
     */
    public static User getUser() {
        String token = getToken();
        if ("admin".equals(token)) {
            User result = new User();
            result.setUserId("admin");
            result.setUserName("管理员");
            return result;
        }
        return LoginCache.getUser(token);

    }

    public static String getToken() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        // 1. 首先从header中获取
        String token = request.getHeader(LoginCache.TOKEN);
        if (StrUtil.isBlank(token)) {
            // 2. 不存在从参数中获取
            token = request.getParameter(LoginCache.TOKEN);
        }
        if (StrUtil.isBlank(token)) {
            // 3. 不存在从cookie中获取
            Cookie[] cookies = request.getCookies();
            if (null != cookies) {
                for (Cookie cookie : cookies) {
                    if (LoginCache.TOKEN.equals(cookie.getName())) {
                        token = cookie.getValue();
                        break;
                    }
                }
            }
        }
        if (StrUtil.isBlank(token)
                && StrUtil.contains(request.getRequestURL(), "localhost")) {
            token = "lemon";
        }
        return token;
    }

}